package com.dominator.serviceImpl;

import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.enums.SocketEnums;
import com.dominator.mybatis.dao.T_gua_machineDao;
import com.dominator.service.ApiWatchingMachineService;
import com.dominator.socket.SocketMessage;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.dao.RedisUtil;
import com.dominator.utils.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("ApiWatchingMachineService")
public class ApiWatchingMachineServiceImpl implements ApiWatchingMachineService {


   private  static  String routerName="ApiWatchingMachineService";


   private static RedisUtil ru=RedisUtil.getRu();


   @Autowired
    T_gua_machineDao tGuaMachineDao;





    /**
     * 初始化
     * @param dto
     * @return
     * @throws ApiException
     */
    @Override
    public SocketMessage init(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"guaMachineId,ticketBoxId,indexNum");
        Map<String, String> map = new HashMap<String, String>();
        map.put("sessionId", dto.getString("sessionId'"));
        map.put("guaMachineId",dto.getString("guaMachineId"));
        map.put("ticketBoxId",dto.getString("ticketBoxId"));
        map.put("indexNum",dto.getString("indexNum"));
        ru.hmset("guaMachine:ms_" + dto.getString("guaMachineId"), map);
        map.clear();
        map.put("guaMachineId",dto.getString("guaMachineId"));
        ru.hmset("guaSession:ms_" + dto.getString("sessionId"), map);
        return new SocketMessage(SocketEnums.REQ_SUCCESS,"init",routerName);

    }

    @Override
    public SocketMessage notificationLogisticsCode(Dto dto) throws ApiException {
        return new SocketMessage(SocketEnums.NOTIFICATION_MAIN_CAMERA_SHOOTING,"notificationLogisticsCode",routerName);
    }

    @Override
    public SocketMessage subLogisticsCode(Dto dto) throws ApiException {
        //todo 保存当前票号
        ApiUtils.checkParam(dto,"logisticsCode,guaMachineId");
        String sessionId = ru.hmget("guaMachine:ms_" + dto.getString("guaMachineId"), "sessionId").get(0);
        String ticketBoxId = ru.hmget("guaMachine:ms_" + dto.getString("guaMachineId"), "ticketBoxId").get(0);





        //通知下一步

        return null;
    }

    @Override
    public SocketMessage notificationMainCameraShooting(Dto dto) throws ApiException {
        return new SocketMessage(SocketEnums.NOTIFICATION_MAIN_CAMERA_SHOOTING,"notificationMainCameraShooting",routerName);
    }

    @Override
    public SocketMessage mainShootingOver(Dto dto) throws ApiException {
        return null;
    }

    @Override
    public SocketMessage notificationSubCameraShooting(Dto dto) throws ApiException {
        return new SocketMessage(SocketEnums.NOTIFICATION_SUB_CAMERA_SHOOTING,"notificationSubCameraShooting",routerName);
    }

    @Override
    public SocketMessage subShootingOver(Dto dto) throws ApiException {
        return null;
    }



    /**
     * 删除所有的状态
     * @param sessionId
     */
    public  void  delAllSession(String sessionId){
        String machineId = ru.hmget("guaSession:session_" + sessionId, "guaMachineId").get(0);
        if (SystemUtils.isNotEmpty(machineId)) {
            ru.hdel("guaMachine:ms_" + machineId, "sessionId");
        }
        ru.hdel("guaSession:session_" + sessionId, "guaMachined");
        return;
    }


}
